package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import common.ServerResponse;
import service.IProductService;
import service.impl.ProductServiceImpl;

@WebServlet("/product")
public class F_ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		setHead(request,response);
		String type=request.getParameter("type");
		System.out.println("进入前台产品servlet===此时的type为："+type);
		if("list".equals(type)) {
			list(request,response);
		}
		else if("detail".equals(type)) {
			detail(request,response);
		}
		else if("all".equals(type)) {
			all(request,response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	
	//产品列表list
	protected void list(HttpServletRequest request, HttpServletResponse response) {
		String category_id=request.getParameter("category_id");
		String keyword=request.getParameter("keyword");
		String pageNum=request.getParameter("pageNum");
		String pageSize=request.getParameter("pageSize");
		String order_by=request.getParameter("order_by");
		IProductService p=new ProductServiceImpl();
		getjson(p.F_list(category_id,keyword,pageNum,pageSize,order_by),response);
	}
	
	//产品detail
	protected void detail(HttpServletRequest request, HttpServletResponse response) {
		String product_id=request.getParameter("product_id");
		IProductService p=new ProductServiceImpl();
		getjson(p.F_detail(product_id),response);
		
	}
	
	//查询所有商品
	protected void all(HttpServletRequest request, HttpServletResponse response) {
		IProductService p=new ProductServiceImpl();
		getjson(p.all(),response);
		
	}
	
	
	protected void setHead(HttpServletRequest request, HttpServletResponse response) {
		//转发时候防止中文乱码
	    response.setHeader("content-type", "text/html;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    String originHeader=request.getHeader("Origin");
		/* 允许跨域的主机地址 */ response.setHeader("Access-Control-Allow-Origin", originHeader);
		/* 允许跨域的请求方法GET, POST, HEAD 等 */ response.setHeader("Access-Control-Allow-Methods", "*"); 
		/* 重新预检验跨域的缓存时间 (s) */ response.setHeader("Access-Control-Max-Age", "3600");
		/* 允许跨域的请求头 */ response.setHeader("Access-Control-Allow-Headers", "*"); 
		/* 是否携带cookie */ response.setHeader("Access-Control-Allow-Credentials", "true");
	}
	
	protected void getjson(ServerResponse se,HttpServletResponse response) {
		Gson gson=new Gson();
		String json=gson.toJson(se);
		PrintWriter pw;
		try {
			pw=response.getWriter();
			pw.write(json);
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

}
